package com.syt.eduservice.controller;


import com.syt.commonutils.R;
import com.syt.eduservice.entity.EduVideo;
import com.syt.eduservice.feignclient.VodClient;
import com.syt.eduservice.service.EduVideoService;
import com.syt.servicebase.exceptionHandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author SYT
 * @since 2021-03-04
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;
    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    //删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id获取视频id
        EduVideo video = videoService.getById(id);
        String videoId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(video)){
            //根据视频ID,远程调用实现视频删除
            R result = vodClient.removeAlyVideo(videoId);
            if (result.getCode()==20001){
                throw new MyException(20001,"删除失败,熔断器...");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }
    //根据Id查询
    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable("videoId")String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("videoInfo",video);
    }
    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }

}

